"use strict";

var WaterPipeHero = function () {};

WaterPipeHero.prototype = {
    init: function () {},
    getUser: function(){
        var address = Blockchain.transaction.from;
        return address;
    },
    getUserInfo: function() {
        var address = Blockchain.transaction.from;
        return LocalContractStorage.get(address);
    },
    getCoin: function() {
        var address = Blockchain.transaction.from;
        var userInfo = {
            id: Blockchain.transaction.timestamp,
            address: address,
            TipsCoin: 100,
            word:'',
            message:[]
        }

        if(!LocalContractStorage.get(address)){
            LocalContractStorage.set(address, JSON.stringify(userInfo));
        }else{
            return 404; //信息找不到
        }

    },
    useCoin: function(userInfo){
        var userInfoC = JSON.parse(userInfo);
        var num = 30;
        if(userInfoC.TipsCoin > num){
            userInfoC.TipsCoin = userInfoC.TipsCoin - num;
            LocalContractStorage.del(userInfoC.address);
            LocalContractStorage.set(userInfoC.address, JSON.stringify(userInfoC));
        }else{
            return 202; //余额不足
        }
        
    },
    askCoin: function(userInfo, userInfoWord){
        var userInfoC = JSON.parse(userInfo);
        userInfoC.word = userInfoWord
        LocalContractStorage.del(userInfoC.address);
        LocalContractStorage.set(userInfoC.address, JSON.stringify(userInfoC));
    },
    morecoin: function(userInfo, num){
        var contractAddress = 'n1Lw9JYSpWv49DbeMpKGXz58MxAW9WQPN36';
        var value = Blockchain.transaction.value;

        var userInfoC = JSON.parse(userInfo);
        userInfoC.TipsCoin = num
        LocalContractStorage.del(userInfoC.address);
        LocalContractStorage.set(userInfoC.address, JSON.stringify(userInfoC));
        Blockchain.transfer(contractAddress,value);
    },
    giveCoin: function (userInfo, myInfo, message, num) {
        var userInfoC = JSON.parse(userInfo);
        var myInfoC = JSON.parse(myInfo);
        if(myInfo.TipsCoin > num){
            myInfo.TipsCoin = myInfo.TipsCoin - num;
            userInfoC.TipsCoin = userInfoC.TipsCoin + num;
            myInfo.message.push({'wordInfo':`您给${userInfoC.address}打赏了${num}个TipCoin`});
            userInfoC.message.push({'wordInfo':`您获得了${myInfo.address}给您打赏的${num}个TipCoin`});
            LocalContractStorage.del(userInfoC.address);
            LocalContractStorage.set(userInfoC.address, JSON.stringify(userInfoC));
            LocalContractStorage.del(myInfoC.address);
            LocalContractStorage.set(myInfoC.address, JSON.stringify(myInfoC));
        }else{
            return 202;
        }
        
    },
    set: function (obj) {
        var defaultData = JSON.parse(LocalContractStorage.get('infoList'));
        var data = Object.prototype.toString.call(defaultData) == '[object Array]' ? defaultData : [];
        data.push({
            id: Blockchain.transaction.timestamp,
            address: obj.address,
            username: obj.username,
            active: obj.active,
            step: obj.step
        });
        if (data.length > 1) {
            LocalContractStorage.del('infoList');
        };
        LocalContractStorage.set('infoList', JSON.stringify(data));
    },
    get: function () {
        return LocalContractStorage.get('infoList');
    },
};

module.exports = WaterPipeHero;